package com.ssm.controller;

import com.ssm.converter.IntegerArrayConverter;
import com.ssm.pojo.DetailShoppingCart;
import com.ssm.pojo.Good;
import com.ssm.pojo.Shoppingcart;
import com.ssm.pojo.User;
import com.ssm.service.GoodService;
import com.ssm.service.ShoppingcartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: exercise1_online_shopping
 * @author: qin haojun
 * @create: 2021-02-14 18:21
 */
@Controller
@RequestMapping("/shoppingcart")
public class ShoppingCartController {

    @Autowired
    ShoppingcartService shoppingcartService;
    @Autowired
    GoodService goodService;

    /**
     * @param goodId
     * @param status false表示只是加入购物车，true表示已经买了
     * @param userId
     * @param number 购买的数量
     * @return
     * @throws Exception
     */
    @RequestMapping("/buy")
    public String buy(@RequestParam(value = "gid") Integer goodId,
                      @RequestParam(value = "status") Boolean status,
                      @RequestParam(value = "uid") Integer userId,
                      @RequestParam(value = "number") Integer number) throws Exception {
//        ModelAndView modelAndView=new ModelAndView();
        //更新商品数据
        Good good = goodService.selectGoodById(goodId);
        goodService.updateTotalById(goodId, good.getTotal() - number);

        //添加购物车在数据库上
        Shoppingcart shoppingcart = new Shoppingcart();
        shoppingcart.setStatus(status);
        shoppingcart.setUid(userId);
        shoppingcart.setGid(goodId);
        shoppingcart.setTotal(number);
        shoppingcartService.addShoppingCart(shoppingcart);
//        modelAndView.setViewName("redirect:/good?gid="+goodId);
        return "redirect:/good?gid=" + goodId;
    }


    /**
     * @param request
     * @param action  值为paid代表以购买的列表，为nopaid代表购物车的东西
     * @return
     */
    @RequestMapping("")
    public ModelAndView getShoppingCart(HttpServletRequest request, String action)
            throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        //要想进入此页面，需要先验证是否登陆了
        if (!isLogin(request)) {
            request.getSession().setAttribute("status", "请先进行登陆");
            modelAndView.setViewName("redirect:/user/login");
            return modelAndView;
        }

        //获得用户id以便查找特定用户的购物车
        Integer userId = ((User) request.getSession().getAttribute("user")).getUid();
        List<DetailShoppingCart> detailShoppingCartList = new ArrayList<>();
        //根据action情况选择页面
        if (action.equals("nopaid")) {
            //添加未购买的货物
            for (DetailShoppingCart cart : shoppingcartService.selectDetailShoppingCart(userId)) {
                if (!cart.getStatus()) {
                    detailShoppingCartList.add(cart);
                }
            }

            modelAndView.addObject("shoppingcart", detailShoppingCartList);
            modelAndView.setViewName("shoppingcart/nopaidcart");
            return modelAndView;
        }
        else if (action.equals("paid")) {
            //添加已购买的货物
            for (DetailShoppingCart cart : shoppingcartService.selectDetailShoppingCart(userId)) {
                if (cart.getStatus()) {
                    detailShoppingCartList.add(cart);
                }
            }

            modelAndView.addObject("shoppingcart", detailShoppingCartList);
            modelAndView.setViewName("shoppingcart/paidcart");
            return modelAndView;
        }
        else {
            modelAndView.setViewName("redirect:/index.jsp");
            return modelAndView;
        }

//        return modelAndView;
    }

    private boolean isLogin(HttpServletRequest request) {
        return request.getSession().getAttribute("user") == null ? false : true;
    }

    @RequestMapping("/delete")
    public String deleteCart(@RequestParam(value = "id") Integer cartId,
                             @RequestParam(value = "action")String action) throws Exception {
        shoppingcartService.deleteShoppingCartById(cartId);
        return "redirect:/shoppingcart?action="+action;
    }

    //
    @RequestMapping("/cartbuy")
    public String cartBuy(@RequestParam(value = "id") Integer[] cartId) throws Exception {
        for (Integer var1 : cartId) {
            shoppingcartService.updateStatusById(var1,true);
        }
        return "redirect:/shoppingcart?action=nopaid";
    }


}
